package com.ct.ecommerce.goods.service;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.ecommerce.api.goods.client.GoodsService;
import com.ct.ecommerce.api.goods.dto.GoodsDTO;
import com.ct.ecommerce.api.goods.dto.GoodsQueryDTO;
import com.ct.ecommerce.api.idgen.client.IdGeneratorService;
import com.ct.ecommerce.framework.core.BeanCopierUtils;
import com.ct.ecommerce.framework.core.response.PageUI;
import com.ct.ecommerce.framework.enums.ECommerceCodeEnum;
import com.ct.ecommerce.goods.dao.entity.GoodsEntity;
import com.ct.ecommerce.goods.dao.mapper.GoodsMapper;
import com.google.common.base.Preconditions;
import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author changtong
 * @since 2021/5/19
 */
@DubboService
@Slf4j
public class GoodsServiceImpl implements GoodsService {


    @DubboReference
    private IdGeneratorService idGeneratorService;

    @Resource
    private GoodsMapper goodsMapper;


    @Override
    public GoodsDTO findGoodsById(Long goodsId) {
        GoodsEntity goodsEntity = goodsMapper.selectById(goodsId);
        Preconditions.checkNotNull(goodsEntity, ECommerceCodeEnum.GOODS_NO_EXIST.getMessage());
        return BeanCopierUtils.copyProperties(goodsEntity, GoodsDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deductGoodsStock(List<GoodsDTO> params) {
        params.forEach(g->{
            GoodsEntity goodsEntity = goodsMapper.selectById(g.getId());
            Preconditions.checkNotNull(goodsEntity,"商品不存在");
            if (g.getStock().compareTo(goodsEntity.getStock()) > 0) {
                throw new IllegalArgumentException(String.format("商品【%s】库存不足", goodsEntity.getName()));
            }
            BigDecimal stock = goodsEntity.getStock().subtract(g.getStock());
            LambdaUpdateWrapper<GoodsEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(GoodsEntity::getId, goodsEntity.getId())
                .set(GoodsEntity::getStock, stock);
            goodsMapper.update(null, updateWrapper);
        });
//        if(!params.isEmpty()){
//            throw new IllegalArgumentException(String.format("商品【%s】库存不足"));
//        }
    }

    @Override
    public PageUI<GoodsDTO> findGoodsPage(GoodsQueryDTO query) {
        IPage<GoodsEntity> iPage = new Page<>(query.getPage(),query.getRows());
        QueryWrapper<GoodsEntity> queryWrapper = new QueryWrapper<>();

        IPage<GoodsEntity> data = goodsMapper.selectPage(iPage, queryWrapper);

        PageUI<GoodsDTO> page = new PageUI<>();
        page.setTotal(data.getTotal())
            .setRows(query.getRows())
            .setPage(query.getPage())
            .setRecords(BeanCopierUtils.copyPropertiesOfList(data.getRecords(),GoodsDTO.class));
        return page;
    }
}
